#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    : /process_pool.py
@Time    : 2021-05-20 17:26:03
@Author  : Gray Snail
@Version : 1.0
@Contact : shtao2011@sina.com
@License : Copyright (c) 2021 Gray Snail
@Desc    : None
'''


from multiprocessing import Pool
import time, os, random


def hello(name):
    start = time.time()
    print(f'{name} 开始执行，进程号：{os.getpid()}')
    time.sleep(random.random() * 2)
    r_time = '{:.2f}'.format(time.time() - start)
    print(f'{name} 执行结束，进程号：{os.getpid()}，耗时{r_time}S')


if __name__ == '__main__':
    p = Pool(3)
    for i in range(4):
        p.apply_async(hello, (i,))

    print('*' * 30, '程序开始', '*' * 30)
    p.close()
    p.join()
    print('*' * 30, '程序结束', '*' * 30)
